package com.tron.dobest.java.tronlambda;


import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class Start {

    public static List<LambdaTestData> people;

    static {

        /**
         * 改方法生成的list是Arrays的一个内部类，和new创建的有区别，只能读取数据。
         */
        people = Arrays.asList(
                new LambdaTestData("人员001", 20, 2000.00),
                new LambdaTestData("人员002", 21, 3000.00),
                new LambdaTestData("人员003", 22, 4000.00),
                new LambdaTestData("人员004", 30, 5000.00),
                new LambdaTestData("人员005", 34, 2000.00),
                new LambdaTestData("人员006", 35, 7000.00),
                new LambdaTestData("人员007", 46, 8000.00),
                new LambdaTestData("人员007", 41, 3000.00)
        );
    }

    public static void main(String[] args) {


        Function<LambdaTestData,String> nf = new Function<LambdaTestData, String>() {
            @Override
            public String apply(LambdaTestData lambdaTestData) {
                return lambdaTestData.getName();
            }
        };


        nf = (a)-> a.getName()+"这是新的方法";






        System.out.println("=================================");
        List<String> collect3 = people.stream().map(nf).collect(Collectors.toList());
        System.out.println(collect3);

        System.out.println("=================================");



        //Consumer范型一规定参数类型
        Consumer<String> stringConsumer = System.out::println;
        stringConsumer.accept("");    //无返回值

        //Function范型一规定参数，范型二规定返回值类型
        Function<String, Integer> function = String::length;
        final Integer apply = function.apply("");

        //Supplier范型一规定返回值类型
        Supplier<String> stringSupplier = String::new;
        final String s = stringSupplier.get();

        //Predicate范型一规定参数类型
        Predicate<String> stringPredicate = x -> false;
        final boolean test = stringPredicate.test("");


        /**
         * 关于排序
         */
        final List<LambdaTestData> collect = people.stream()
                //传递一个比较器 返回值类型要求 U extends Comparable
                .sorted(Comparator.comparing(LambdaTestData::getAge).reversed())
                .collect(Collectors.toList());
        System.out.println(collect);

        //如果集合内的类型没有实现Comparable接口，则需要指定一个比较器
        Collections.sort(people, Comparator.comparing(LambdaTestData::getAge));

        /**
         * 去重复
         */
        final Set<LambdaTestData> collect1 = new HashSet<>(people);
        System.out.println("------------------根据名称去重前---------------------");
        collect1.forEach(System.out::println);

        System.out.println("----------------根据名称去重后---------------");

        final ArrayList<LambdaTestData> collect2 = people.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(
                                () -> new TreeSet<>(Comparator.comparing(o -> o.getName()))),
                        ArrayList::new));
        collect2.forEach(System.out::println);

        /**
         * 分組
         */

        Map<String, Map<String, List<LambdaTestData>>> group = people.stream().collect(Collectors.groupingBy(
                e -> {
                    if (e.getAge() < 30) {
                        return "少儿组";
                    } else if (e.getAge() < 40) {
                        return "壮丁组";
                    } else {
                        return "成年组";
                    }
                }, Collectors.groupingBy(e -> {
                    if (e.getSalary() >= 3000) {
                        return "有钱人";
                    }
                    return "穷人";
                })
        ));
        System.out.println(group);


    }


}
